//
//  HomeViewController.m
//  CouponManager
//
//  Created by BoHyun Jo on 11. 5. 16..
//  Copyright 2011 CJ오쇼핑. All rights reserved.
//

#import "HomeViewController.h"

#import "Const.h"
#import "CouponData.h"
#import "CouponDetailViewController.h"
#import "MyCouponCell.h"
@implementation HomeViewController
@synthesize tView;
@synthesize cmArray,dArray;
////
-(IBAction)deletaButton:(id)sender{
    NSLog(@"deleteButton");
    isDeleteState = !isDeleteState;
    [self.tView reloadData];
}
-(void)test{
    cmArray = [[NSMutableArray alloc]init];
    dArray = [[NSMutableArray alloc]init];
    for(int i = 0 ; i < 10 ; i++){
        
        CouponData* cData = [[CouponData alloc]init];
        cData.title = [NSString stringWithFormat:@"%d Title",i];
        [cmArray addObject:cData];
        
    }
    for(int i = 0 ; i < 2 ; i++){
        
        CouponData* cData = [[CouponData alloc]init];
        cData.title = [NSString stringWithFormat:@"%d Title",i];
        [dArray addObject:cData];
        
    }
    [tView reloadData];
    NSLog(@"%@",cmArray);
}
////

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
  
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [tView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
        [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = deleteButton;
    [self test];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:NAVI_R green:NAVI_G blue:NAVI_B alpha:NAVI_A];
    self.navigationController.delegate = self;

    
     //클립보드 문자열 가져오기 
    UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
    NSLog(@"%@",[appPasteBoard string]);
    ///
    //아이콘에 뱃지 달기
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    ////
    
    
    //현자시간 가져오기
    NSDate *now = [[NSDate alloc] init];
    
    // 날짜 포맷.
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    
    // 시간 포맷.
    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"HH:mm:ss"];
    
    NSString *theDate = [dateFormat stringFromDate:now];
    NSString *theTime = [timeFormat stringFromDate:now];
    NSString *updated = @"Updated: ";
    updated = [updated stringByAppendingString:theDate];
    updated = [updated stringByAppendingString:@" "];
    updated = [updated stringByAppendingString:theTime];
    
    NSLog(@"Updated: %@", updated);
    
    
    //[tView setEditing:YES];
    
    [dateFormat release];
    [timeFormat release];
    [now release];
    
    //////
    
    

    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [tView release];
    tView = nil;
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark UITableViewDelegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
    CouponDetailViewController* couponDetailViewController = [[CouponDetailViewController alloc] initWithNibName:@"CouponDetailViewController" bundle:nil];
    [couponDetailViewController setCouponData:[cmArray objectAtIndex:indexPath.row]];
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:couponDetailViewController animated:YES];
    self.hidesBottomBarWhenPushed = NO;    
}

#pragma mark UITableViewDataSource
-(void)cellDeleteButton:(id)sender{
    NSString* cellStr = [(UIButton*)sender titleForState:UIControlStateDisabled];
    NSArray *cellInfo = [cellStr componentsSeparatedByString:@"_"];
    if([[cellInfo objectAtIndex:0]intValue]==0){
        [dArray removeObjectAtIndex:[[cellInfo objectAtIndex:1]intValue]];
    }else if([[cellInfo objectAtIndex:0]intValue]==1){
        [cmArray removeObjectAtIndex:[[cellInfo objectAtIndex:1]intValue]];
    }
    [self.tView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"MyCouponCell";
    
    MyCouponCell *cell = (MyCouponCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
		NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"MyCouponCell" owner:nil options:nil];
		cell = [arr objectAtIndex:0];
	}
    
    CouponData* cdata = [cmArray objectAtIndex:indexPath.row];
    [cell.delButton setHidden:!isDeleteState];
    [cell.delButton setTitle:[NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row] forState:UIControlStateDisabled];
    [cell.delButton addTarget:self action:@selector(cellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"%@",[cell.delButton titleForState:UIControlStateDisabled]);
    //[cell.textLabel setText:cdata.title];
    return  cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return [dArray count];
    }else if(section == 1){
        return [cmArray count];
    }
    return 0;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 여기서 항목 삭제
    [cmArray removeObjectAtIndex:indexPath.row];
    [tView reloadData];
}


 
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // 여기서 항목 이동
    
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section==0){
        sectionLabel1.text = [NSString stringWithFormat:@"마감임박 쿠폰 %d",[dArray count]];
        return section1;   
    }
    else if(section==1){
        sectionLabel2.text = [NSString stringWithFormat:@"사용가능 쿠폰 %d",[cmArray count]];
        return section2;   
    }

}

#pragma mark SegmentControl
-(IBAction)segmentControl:(id)sender{
    if(sender == seg1){
        [seg1 setSelected:YES];
        [seg2 setSelected:NO];
        [seg3 setSelected:NO];
    }else if(sender == seg2){
        [seg1 setSelected:NO];
        [seg2 setSelected:YES];
        [seg3 setSelected:NO];
    }else if(sender == seg3){
        [seg1 setSelected:NO];
        [seg2 setSelected:NO];
        [seg3 setSelected:YES];
    }
}


#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if([viewController isKindOfClass:[HomeViewController class]]){

        NSLog(@"homeViewController");
        HomeViewController* homeViewController = (HomeViewController*)viewController;
        UITableViewCell* cell = [homeViewController.tView cellForRowAtIndexPath:[homeViewController.tView indexPathForSelectedRow]];
        [cell setSelected:NO animated:YES];
    }
}

@end
